/*
*这是C语言链表，vs2010编译通过
*
*/
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0

typedef int elementType;
typedef int status;
typedef struct Sqlist
{
	int id;
	elementType data;
	Sqlist *next;
}Sqlist,*linkToSqlist;

Sqlist *createList();
Sqlist *findElement(Sqlist *,int);
status ifElementExist(Sqlist *,int);
status ifListExist(Sqlist *);
status ifSuccess(status);
elementType findElementById(Sqlist *,int);
int findIdByElement(Sqlist *,elementType);
int findPositionByElement(Sqlist *,elementType);
status addToAListEnd(Sqlist *,elementType);
status insertIntoAList(Sqlist *,int,elementType);
status addAElementToList(Sqlist *,int,elementType);
status printAllElement(Sqlist *);
status deleteAElement(Sqlist *,int);
status deleteAList(Sqlist *);

int main()
{
	int i = 0,sti = 0,noError = 1;
	while(0 <= i && i <= 8)
	{
		printf("------------\n");
		printf("链表表操作：\n");
		printf("1 创建一个新表\n");
		printf("2 销毁一个表\n");
		printf("3 在表中添加一个元素\n");
		printf("4 从表中删除指定位置的元素\n");
		printf("5 从表中查找指定位置的元素\n");
		printf("6 查找元素在表中的位置\n");
		printf("7 检查表是否存在\n");
		printf("8 打印表中的所有元素\n");
		int id = -1;
		Sqlist *list;
		elementType element;
		if(!scanf("%d",&i))
		{
			printf("输入错误\n");
			noError = 0;
			getchar();
		}else
		{
			noError = 1;
		}
		if(sti)
		{
			switch(i)
			{
				case 1:
				printf("链表已创建\n");
				break;
				case 2:
				printf("销毁一个表\n");
				ifSuccess(deleteAList(list));
				sti = 0;
				break;
				case 3:
				printf("在表中添加一个元素\n请输入元素和位置");
				scanf("%d%d",&element,&id);
				ifSuccess(addAElementToList(list,id,element));
				break;
				case 4:
				printf("从表中删除指定位置的元素\n请输入元素位置");
				scanf("%d",&id);
				ifSuccess(deleteAElement(list,id));
				break;
				case 5:
				printf("从表中查找指定位置的元素\n请输入元素位置");
				scanf("%d",&element);
				printf("%d\n",findElementById(list,element));
				break;
				case 6:
				printf("查找元素在表中的位置\n请输入元素");
				scanf("%d",&element);
				printf("ElementPosition:%d\n",findPositionByElement(list,element));
				break;
				case 7:
				printf("检查表是否存在\n");
				if(ifListExist(list))
				{
					printf("链表已创建\n");
				}else
				{
					printf("链表不存在\n");
				}
				break;
				case 8:
				printf("打印表中的所有元素\n");
				ifSuccess(printAllElement(list));
				break;
				default:
				printf("请输入合适的操作\n");
				noError = 0;
			}
		}else if(noError)
		{
			if(i != 1) printf("表还没有创建，请先创建一个表\n");
			printf("创建一个链表\n");
			list = createList();
			sti = 1;
			printf("链表已创建\n");
		}
		system("pause");
		i = 0;
	}
	system("pause");
	return 0;
}

Sqlist *createList()
{
	Sqlist *list;
	Sqlist *head = new Sqlist;
	list = head;
	head -> id = 0;
	head -> data = 0;
	head -> next = NULL;
	return list;
}

elementType findElementById(Sqlist *list,int id)
{
	if(ifElementExist(list,id))
	{
		return findElement(list,id) -> data;
	}else
	{
		printf("Out Of List Range\n");
		return 0;
	}
}

int findIdByElement(Sqlist *list,elementType element)
{
	if(ifListExist(list))
	{
		Sqlist *p = list -> next;
		while(p)
		{
			if(p -> data == element)
			{
				return p -> id;
			}
			p = p -> next;
		}
	}else
	{
		printf("List Do Not Exist\n");
		return -1;
	}
	printf("Element NOT Found In This List\n");
	return -1;
}

int findPositionByElement(Sqlist *list,elementType element)
{
	if(ifListExist(list))
	{
		Sqlist *p = list -> next;
		int position = 0;
		while(p)
		{
			if(p -> data == element)
			{
				return position;
			}
			p = p -> next;
			position ++;
		}
	}else
	{
		printf("Not Found This Element In List\n");
		return -1;
	}
	return -1;
}

Sqlist *findElement(Sqlist *list,int id)
{
	Sqlist *p = list;
	int my_id = 0;
	while(id >= 0 && my_id < id && p)
	{
		p = p -> next;
		my_id ++;
	}
	if(my_id == id) return p;
	printf("FindElementError\nProcessWillExit");
	exit(1);
}

status ifElementExist(Sqlist *list,int id)
{
	Sqlist *p = findElement(list,id);
	if(p) return TRUE;
	return FALSE;
}

status ifListExist(Sqlist *list)
{
	if(list -> next)
	{
		return TRUE;
	}else
	{
		return FALSE;
	}
}

status ifSuccess(status sta)
{
	if(sta)
	{
		printf("Success\n");
		return TRUE;
	}else
	{
		printf("Fail\n");
		return sta;
	}
}

status addToAListEnd(Sqlist *list,elementType element)
{
	if(list)
	{
		Sqlist *newNode = new Sqlist;
		findElement(list,list -> data) -> next = newNode;
		newNode -> id = list -> data + 1;
		newNode -> data = element;
		newNode -> next = NULL; 
		list -> data ++;
		return TRUE;
	}else
	{
		return FALSE;
	}
}

status insertIntoAList(Sqlist *list,int id,elementType element)
{
	if(ifElementExist(list,id - 1) && ifElementExist(list,id))
	{
		Sqlist *newNode = new Sqlist;
		newNode -> id = list -> data + 1;
		newNode -> data = element;
		newNode -> next = findElement(list,id);
		findElement(list,id - 1) -> next = newNode;
		list -> data ++;
		return TRUE;
	}else
	{
		return FALSE;
	}
}

status addAElementToList(Sqlist *list,int id,elementType element)
{
	if(id == list -> data + 1)
	{
		return addToAListEnd(list,element);
	}else
	{
		return insertIntoAList(list,id,element);
	}
}

status printAllElement(Sqlist *list)
{
	if(ifListExist(list))
	{
		int i = 0;
		Sqlist *p = list -> next;
		while(i < 10 && p)
		{
			//printf("ID:%d ",p -> id);
			printf("[%d] ",p -> data);
			printf("%x -> ",p -> next);
			p = p -> next;
			i ++;
		}
		return TRUE;
	}else
	{
		printf("List Have NO Element\n");
		return FALSE;
	}
}

status deleteAElement(Sqlist *list,int id)
{
	if(ifElementExist(list,id))
	{
		Sqlist *p = findElement(list,id - 1);
		Sqlist *q = p -> next;
		Sqlist *r = q -> next;
		if(id == list -> data)
		{
			p -> next = NULL;
		}else
		{
			 p -> next = r;
		}
		delete q;
		list -> data --;
		return TRUE;
	}else
	{
		return FALSE;
	}
}

status deleteAList(Sqlist *list)
{
	if(list)
	{
		Sqlist *p = list;
		while(p)
		{
			p = p -> next;
			delete p;
		}
		list -> next = NULL;
		list -> data = 0;
		return TRUE;
	}else
	{
		return FALSE;
	}
}